<template>
  <!-- <div id="app">
    <my-add></my-add>
    <my-sub></my-sub>
  </div>-->

  <!--案例的代码 -->
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handelinpuy" />
    <a-button type="primary" @click="addEvent">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e) => {falseOrTrue(e, item.id)}">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItemId(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{undone}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey === 'all' ? 'primary':'default'" @click="changeList('all')">全部</a-button>
          <a-button :type="viewKey === 'notdone' ? 'primary':'default'" @click="changeList('notdone')">未完成</a-button>
          <a-button :type="viewKey === 'done' ? 'primary':'default'" @click="changeList('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="removeDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
// import Add from './components/add.vue'
// import Sub from './components/sub.vue'
// export default {
// data () {
// return {}
// },
// components: {
// 'my-add': Add,
// 'my-sub': Sub
// }
// }

// 案例的代码
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {}
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['inputValue', 'viewKey']),
    ...mapGetters(['undone', 'infoList'])
  },
  methods: {
    handelinpuy (e) {
      // console.log(e.target.value)
      this.$store.commit('inputValueEvent', e.target.value)
    },
    addEvent () {
      if (this.inputValue.trim().length === 0) {
        return this.$message.warning('请输入内容')
      }
      this.$store.commit('addItem')
    },
    removeItemId (id) {
      // console.log(id)
      this.$store.commit('remoeItem', id)
    },
    falseOrTrue (e, id) {
      const param = {
        id: id,
        status: e.target.checked
      }
      this.$store.commit('statusChange', param)
    },
    removeDone () {
      this.$store.commit('clean')
    },
    changeList (key) {
      this.$store.commit('changeViewKey', key)
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
